package com.engine4cloud.tdf.supervision.controller.web;

import cn.hutool.core.collection.CollUtil;
import com.engine4cloud.tdf.common.core.constant.enums.CorElectronicFenceRuleTypeEnum;
import com.engine4cloud.tdf.common.core.constant.enums.ElectronicFenceTypeEnum;
import com.engine4cloud.tdf.common.core.util.R;
import com.engine4cloud.tdf.common.data.web.response.PageInfo;
import com.engine4cloud.tdf.common.data.web.response.Params;
import com.engine4cloud.tdf.common.log.annotation.SysLog;
import com.engine4cloud.tdf.supervision.dto.create.JzdxElectronicFenceCreateDTO;
import com.engine4cloud.tdf.supervision.dto.query.JzdxElectronicFenceQueryDTO;
import com.engine4cloud.tdf.supervision.dto.result.EnumsTypeResultDTO;
import com.engine4cloud.tdf.supervision.dto.result.JzdxCustomFenceSelectResultDTO;
import com.engine4cloud.tdf.supervision.dto.result.JzdxElectronicFenceResultDTO;
import com.engine4cloud.tdf.supervision.dto.update.JzdxElectronicFenceUpdateDTO;
import com.engine4cloud.tdf.supervision.service.JzdxElectronicFenceService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 矫正对象-电子围栏管理-web
 *
 * @author wenjie
 * @date 2024-04-22 16:05:12
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/web/electronic/fence")
@Tag(description = "ElectronicFence", name = "矫正对象-电子围栏管理-web")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class JzdxElectronicFenceWebController {

    private final JzdxElectronicFenceService jzdxElectronicFenceService;

    /**
     * 规则列表
     */
    @Operation(summary = "规则列表", description = "规则列表")
    @GetMapping("/all/rule")
    public R<List<EnumsTypeResultDTO>> rule() {
        List<EnumsTypeResultDTO> labelList = Arrays.stream(CorElectronicFenceRuleTypeEnum.values())
                .map(state -> new EnumsTypeResultDTO(state.getType(), state.getDesc()))
                .toList();
        return R.ok(labelList);
    }

    /**
     * 分页查询已有的自定义围栏
     *
     * @param page     分页查询已有的自定义围栏
     * @param keywords 围栏名称
     * @return R<PageInfo < JzdxElectronicFenceResultDTO>>
     */
    @Operation(summary = "分页查询已有的自定义围栏", description = "分页查询已有的自定义围栏")
    @GetMapping("/custom/page")
    public R<PageInfo<JzdxCustomFenceSelectResultDTO>> customPageList(@ParameterObject Params page, @Valid @ParameterObject String keywords) {
        return R.ok(jzdxElectronicFenceService.customPageList(page, ElectronicFenceTypeEnum.CUSTOM, keywords));
    }

    /**
     * 分页查询已有的电子围栏
     *
     * @param page 分页查询已有的电子围栏
     * @param dto  矫正对象电子围栏
     * @return R<PageInfo < JzdxElectronicFenceResultDTO>>
     */
    @Operation(summary = "分页查询", description = "分页查询")
    @GetMapping("/page")
    public R<PageInfo<JzdxElectronicFenceResultDTO>> pageList(@ParameterObject Params page, @Valid @ParameterObject JzdxElectronicFenceQueryDTO dto) {
        return R.ok(jzdxElectronicFenceService.pageList(page, dto));
    }


    /**
     * 通过id查询矫正对象电子围栏
     *
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询", description = "通过id查询")
    @GetMapping("/{id}")
    public R<JzdxElectronicFenceResultDTO> getById(@PathVariable("id") Long id) {
        return R.ok(jzdxElectronicFenceService.getById(id));
    }

    /**
     * 新增矫正对象电子围栏
     *
     * @param dto 矫正对象电子围栏
     * @return R
     */
    @Operation(summary = "新增矫正对象电子围栏", description = "新增矫正对象电子围栏")
    @SysLog("新增矫正对象电子围栏")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('electronic_fence_add')")
    public R<Boolean> create(@Valid @RequestBody JzdxElectronicFenceCreateDTO dto) {
        return R.ok(jzdxElectronicFenceService.create(dto));
    }

    /**
     * 更新矫正对象电子围栏
     *
     * @param dto 矫正对象电子围栏
     * @return R
     */
    @Operation(summary = "更新矫正对象电子围栏", description = "更新矫正对象电子围栏")
    @SysLog("更新矫正对象电子围栏")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('electronic_fence_edit')")
    public R<Boolean> update(@Valid @RequestBody JzdxElectronicFenceUpdateDTO dto) {
        return R.ok(jzdxElectronicFenceService.update(dto));
    }

    /**
     * 通过id删除矫正对象电子围栏
     *
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除矫正对象电子围栏", description = "通过id删除矫正对象电子围栏")
    @SysLog("通过id删除矫正对象电子围栏")
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('electronic_fence_del')")
    public R<Boolean> removeById(@RequestBody Long[] ids) {
        return R.ok(jzdxElectronicFenceService.removeBatchByIds(CollUtil.toList(ids)));
    }
}